<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
/**
 * SFA消费者二维码-控制器
 * @author 咩咩 <liuyangvsms@163.com>
 */
class QRcodeController extends AdminController {

    /**
     * 消费二维码-总览
     */
    public function index(){
        /*--------------------------测试开始--------------------------*/
//        $name = '名字';
//        error_log('name='.$name."\n", 3, "test.log");//打印日志

//        $db_wash_calculation_function = M('wash_calculation_function');//实例化功能表
//        $info['p_first'] = 0;
//        $edit_p_first_wash_calculation_product = $db_wash_calculation_function->where("p_first=1")->setField($info);
//        if(!$edit_p_first_wash_calculation_product){
//            $this->error('洗涤必备优先状态还原失败!');
//        }

//        $num = 10;
//        for($i=1; $i<=$num; $i++){
//            echo $i;
//        }



//        $QRcode = new \Vendor\qrcode\QRcode();
//        $QRcode_url = 'https://app1.bluemoon.com.cn/sfa?id=A295B0DE65ACB0224'; //二维码链接
//        $errorCorrectionLevel = 'M';//容错级别
//        $matrixPointSize = 3;//生成图片大小
//        //生成二维码图片
//        $QR_path = '../wash_helper/Uploads/QRcode/image/222/';
//        if(!file_exists($QR_path)){
//            mkdir($QR_path, 0777);
//            chmod($QR_path, 0777);
//        }
//        $QR = $QR_path.'qrcode.png';//生成的二维码路径
//        $QRcode->png($QRcode_url, $QR, $errorCorrectionLevel, $matrixPointSize, 2);
//
//        $logo = './Uploads/QRcode/image/test_logo.png';//二维码中的logo图片路径
//        if (file_exists($logo)) {
//            $QR_logo = imagecreatefromstring(file_get_contents($QR));
//            $logo = imagecreatefromstring(file_get_contents($logo));
//            $QR_width = imagesx($QR_logo);//二维码图片宽度
//            $QR_height = imagesy($QR_logo);//二维码图片高度
//            $logo_width = imagesx($logo);//logo图片宽度
//            $logo_height = imagesy($logo);//logo图片高度
//            $logo_qr_width = $QR_width / 4;
//            $scale = $logo_width/$logo_qr_width;
//            $logo_qr_height = $logo_height/$scale;
//            $from_width = ($QR_width - $logo_qr_width) / 2;
//            //重新组合图片并调整大小
//            imagecopyresampled($QR_logo, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
//                $logo_qr_height, $logo_width, $logo_height);
//            //输出二维码水印图
//            imagepng($QR_logo, $QR);
//        }
//        echo '<img src="./Uploads/QRcode/image/111/qrcode.png">';



//        $zip_url_path = './Uploads/QRcode/zip/K20000003P1.zip';
//        if(!file_exists($zip_url_path)){
//            fopen($zip_url_path, "w");
//        }
//
//        //实例化
//        $zip = new \ZipArchive();
//        $ZipFile = new \Vendor\zip\Zip();
//
//        if($zip->open($zip_url_path, \ZipArchive::OVERWRITE)=== TRUE){
//            //调用方法，对要打包的根目录进行操作，并将ZipArchive的对象传递给方法
//            $ZipFile->addFileToZip('./Uploads/QRcode/image/K20000003P1', $zip);
//            $zip->close(); //关闭处理的zip文件
//        }
//
//        /*文件下载*/
//        $file = $zip_url_path;   //文件路径
//        $filename = basename($file); //要下载文件的名字
//        header("Content-Type: application/force-download");
//        header("Content-Disposition: attachment; filename=".($filename));
//        readfile($file);



//        //引入文件
//        require './ThinkPHP/Library/Vendor/TCPDF/tcpdf.php';
//        //实例化
//        $pdf = new \TCPDF();
//        // 设置文档信息
//        $pdf->SetCreator('bluemoon');
//        $pdf->SetAuthor('liuyang');
//        $pdf->SetTitle('消费者二维码 - C10066683P11');
//        $pdf->SetSubject('TCPDF Tutorial');
//        $pdf->SetKeywords('TCPDF, PDF, PHP');
//        // 设置页眉和页脚信息
//        $pdf->SetHeaderData('', '', '蓝月亮（中国）有限公司-消费者二维码                                                                                                    产品编号：C10066683      批次号：11', '', array(0,64,255), array(0,64,128));
//        $pdf->setFooterData(array(0,64,0), array(0,64,128));
//        // 设置页眉和页脚字体
//        $pdf->setHeaderFont(Array('stsongstdlight', '', '9'));
//        $pdf->setFooterFont(Array('helvetica', '', '9'));
//        // 设置默认等宽字体
//        $pdf->SetDefaultMonospacedFont('courier');
//        // 设置间距
//        $pdf->SetMargins(5, 9, 5);
//        $pdf->SetHeaderMargin(2);
//        $pdf->SetFooterMargin(6);
//        // 设置分页
//        $pdf->SetAutoPageBreak(TRUE);
//        //设置自动换页
////        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//        // 设置图片比例
//        $pdf->setImageScale(1.25);
//        // 设置默认字体子集设置模式
//        $pdf->setFontSubsetting(true);
//        //设置字体
//        $pdf->SetFont('stsongstdlight', '', 14);
//        //添加一个页面
//        $pdf->AddPage();
//
////        $pdf->writeHTML('<span><img style="" src="./Uploads/QRcode/image/qrcode.png"></span><span><img src="./Uploads/QRcode/image/qrcode.png"></span>');
//        $pdf->writeHTML('<table cellpadding="0" cellspacing="0" border="0" style="align:center;">
//                                    <tr>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                    </tr>
//                                    <tr>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                        <td><img src="./Uploads/QRcode/image/2000003P1/2000003P1N1.png"></td>
//                                    </tr>
//                                </tr></table>');
//
//        //输出PDF 默认是I：在浏览器中打开，D：下载，F：在服务器生成pdf ，S：只返回pdf的字符串
//        $pdf->Output('./Uploads/QRcode/pdf/ttt.pdf', 'I');


//        $path = './Uploads/QRcode/image/test';
//        $this->delDirAndFile($path);


//        $str = '["32","37","35","31","36","38","53"]';
//        $arr = json_decode($str);
//        print_r($arr);
//        $id = 31;
//        $key =  array_search($id,$arr);
//        unset($arr[$key]);//删除数组元素
//        $list = [];
//        foreach($arr as $v){
//            array_push($list,$v);
//        }
//        print_r($list);
//        $json = json_encode($list);
//        print_r($json);

        /*--------------------------测试结束--------------------------*/

        $this->meta_title = '消费二维码-总览';//页面title信息
        $this->assign('title', '消费二维码-总览');//页面标题
        $this->display();
    }

    /**
     * 产品管理-产品类别
     */
    public function p_class(){
        $db_wash_consumer_class = M('wash_consumer_class');//实例化类别表

        $class_list = $db_wash_consumer_class->field('id,name,p_list')->select();
        $this->assign('list', $class_list);

        $this->meta_title = '产品管理-产品类别';//页面title信息
        $this->assign('title', '产品管理-产品类别');//页面标题
        $this->display();
    }

    /**
     * 产品管理-产品类别
     */
    public function p_class_product_choice(){
        $db_wash_consumer_class = M('wash_consumer_class');//实例化类别表
        $db_wash_consumer_product = M('wash_consumer_product');//实例化产品信息表

        $id = I('get.id');
        $this->assign('id', $id);
        $consumer_class = $db_wash_consumer_class->where("id='$id'")->field('name,p_list')->find();
        $class_name = $consumer_class['name'];
        $this->assign('class_name', $class_name);
        $class_p_list = json_decode($consumer_class['p_list'],true);
        $p_arr = array();
        foreach($class_p_list as $pid){
            $p = $db_wash_consumer_product->where("id='$pid'")->field('id,p_id,p_name,status,name,img_url')->find();
            if($p){
                array_push($p_arr,$p);
            }
        }
        $this->assign('p_arr', $p_arr);

        $this->meta_title = '产品管理-'.$class_name;//页面title信息
        $this->assign('title', '产品管理-'.$class_name);//页面标题
        $this->display();
    }

    /**
     * 产品管理-产品类别-产品选择
     */
    public function p_class_product_choice_edit(){
        $db_wash_consumer_class = M('wash_consumer_class');//实例化类别表
        $db_wash_consumer_product = M('wash_consumer_product');//实例化产品信息表

        $id = I('get.id');
        $this->assign('id', $id);
        $consumer_class = $db_wash_consumer_class->where("id='$id'")->field('name,p_list')->find();
        $class_name = $consumer_class['name'];
        $class_p_list = json_decode($consumer_class['p_list'], true);

        $arr = array();
        $condition['status'] = array('eq', 1);
        $condition['name'] = array('neq', '');
        $condition['img_url'] = array('neq', '');
        $product = $db_wash_consumer_product->where($condition)->order('id DESC')->field('id,p_id,p_name,name,img_url')->select();
        foreach($product as $p){
            if(in_array($p['id'],$class_p_list)){
                $p['checked'] = 1;
            }else{
                $p['checked'] = 0;
            }
            array_push($arr, $p);
        }

        /*按是否选中排序*/
        //先用array_column 多维数组按照纵向（列）取出
        $date = array_column($arr, 'checked');
        //再用array_multisort  结合array_column得到的结果对$arr进行排序
        array_multisort($date,SORT_DESC,$arr);

        $this->assign('arr', $arr);

        if(IS_POST){
            $post_id = I('post.id');
            $post_product = I('post.product');
            if(!$post_product){
                $post_product = array();
            }

            $post_p_list = $post_product;
            $json_post_p_list = json_encode($post_p_list, true);
            //更新洗涤必备数据库
            $info['p_list'] = $json_post_p_list;
            $db_wash_consumer_class->where("id='$post_id'")->setField($info);
        }

        $this->meta_title = '产品管理-'.$class_name.'-产品选择';//页面title信息
        $this->assign('title', '产品管理-'.$class_name.'-产品选择');//页面标题
        $this->display();
    }

    /**
     * 产品管理-产品类别-产品信息-编辑
     */
    public function p_class_product_edit(){
        $db_wash_consumer_product = M('wash_consumer_product');//实例化产品信息表

        $class_id = I('get.class_id');//分类ID

        $id = I('get.id');//产品ID
        $product = $db_wash_consumer_product->where("id='$id'")->find();
        $this->assign('product', $product);

        if(IS_POST){
            $name = I('post.name');

            //判断是否有文件上传
            if($_FILES["file"]["tmp_name"]){
                /*文件上传*/
                $savename = time().'_'.mt_rand(1000000,9999999);
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 20480000;// 设置附件上传大小 2M=2048000
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
                $upload->savePath = './Consumer_sfa/product/picture/'; // 设置附件上传（子）目录
                $upload->saveName = $savename; // 上传文件的文件名
                //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
                $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
                $upload->replace = true; // true会存在同名文件将覆盖
                // 上传文件
                $upload_info = $upload->upload();
                if (!$upload_info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{
                    $img_url = "/wash_helper/Uploads/Consumer_sfa/product/picture/".$upload_info['file']['savename'];
                    $info['img_url'] = $img_url;

                    /*删除文件*/
                    $product_url_path = '..'.$product['img_url'];
                    if(file_exists($product_url_path)){
                        unlink($product_url_path);
                    }
                }
            }

            $info['name'] = $name;
            $info['time'] = time();
            $info['datetime'] = date('Y-m-d H:i:s');
            $edit_wash_consumer_product = $db_wash_consumer_product->where("id='$id'")->setField($info);
            if($edit_wash_consumer_product){
                $this->redirect('QRcode/p_class_product_choice',array('id'=>$class_id));
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '产品管理-产品类别-产品信息-编辑';//页面title信息
        $this->assign('title', '产品管理-产品类别-产品信息-编辑');//页面标题
        $this->display('product_edit');
    }

    /**
     * 产品管理-产品类别-新增
     */
    public function p_class_add(){
        $db_wash_consumer_class = M('wash_consumer_class');//实例化类别表

        if(IS_POST){
            $name = I('post.name');

            $info['name'] = $name;
            $info['p_list'] = '[]';
            $add_wash_consumer_class = $db_wash_consumer_class->data($info)->add();
            if($add_wash_consumer_class){
                $this->redirect('QRcode/p_class');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '产品管理-产品类别-新增';//页面title信息
        $this->assign('title', '产品管理-产品类别-新增');//页面标题
        $this->display();
    }

    /**
     * 产品管理-产品类别-编辑
     */
    public function p_class_edit(){
        $db_wash_consumer_class = M('wash_consumer_class');//实例化类别表

        $id = I('get.id');
        $consumer_class = $db_wash_consumer_class->where("id='$id'")->field('name')->find();
        $this->assign('consumer_class', $consumer_class);

        if(IS_POST){
            $name = I('post.name');

            $info['name'] = $name;
            $edit_wash_consumer_class = $db_wash_consumer_class->where("id='$id'")->setField($info);
            if($edit_wash_consumer_class){
                $this->redirect('QRcode/p_class');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '产品管理-产品类别-编辑';//页面title信息
        $this->assign('title', '产品管理-产品类别-编辑');//页面标题
        $this->display();
    }

    /**
     * 产品管理-产品类别-删除
     */
    public function p_class_del(){
        $db_wash_consumer_class = M('wash_consumer_class');//实例化类别表

        $id = I('get.id');
        $del_wash_consumer_class = $db_wash_consumer_class->where("id='$id'")->delete();
        if($del_wash_consumer_class){
            $this->success('删除成功!');
        }else{
            $this->error('删除失败!');
        }
    }

    /**
     * 产品管理-产品类别-排序向上
     */
    public function p_class_product_up(){
        $db_wash_consumer_class = M('wash_consumer_class');//实例化类别表

        $id = I('get.id');
        $class_id = I('get.class_id');

        $in_arr = array($id);
        $consumer_class = $db_wash_consumer_class->where("id='$class_id'")->field('p_list')->find();
        $arr = json_decode($consumer_class['p_list'],true);
        $key =  array_search($id,$arr);
        if($key == 0){
            $this->error('已经是第一个，不能再继续操作了哦~');
        }else{
            unset($arr[$key]);//删除数组元素
//            error_log('$up_arr='.json_encode($arr, true)."\n", 3, "test.log");//打印日志

            $new_key = $key - 1;
            array_splice($arr, $new_key, 0, $in_arr);
//            error_log('$up_arr='.json_encode($arr, true)."\n", 3, "test.log");//打印日志

            $info['p_list'] = json_encode($arr, true);
            $edit_wash_consumer_class = $db_wash_consumer_class->where("id='$class_id'")->setField($info);
            if($edit_wash_consumer_class){
                $this->success('操作成功~');
            }else{
                $this->error('操作失败，请稍后再试！');
            }
        }
    }

    /**
     * 产品管理-产品类别-排序向下
     */
    public function p_class_product_down(){
        $db_wash_consumer_class = M('wash_consumer_class');//实例化类别表

        $id = I('get.id');
        $class_id = I('get.class_id');

        $in_arr = array($id);
        $consumer_class = $db_wash_consumer_class->where("id='$class_id'")->field('p_list')->find();
        $arr = json_decode($consumer_class['p_list'],true);
        $key =  array_search($id,$arr);
        $total_key = count($arr) - 1;
        if($key == $total_key){
            $this->error('已经是最后一个，不能再继续操作了哦~');
        }else{
            unset($arr[$key]);//删除数组元素
//            error_log('$down_arr='.json_encode($arr, true)."\n", 3, "test.log");//打印日志

            $new_key = $key + 1;
            array_splice($arr, $new_key, 0, $in_arr);
//            error_log('$down_arr='.json_encode($arr, true)."\n", 3, "test.log");//打印日志

            $info['p_list'] = json_encode($arr, true);
            $edit_wash_consumer_class = $db_wash_consumer_class->where("id='$class_id'")->setField($info);
            if($edit_wash_consumer_class){
                $this->success('操作成功~');
            }else{
                $this->error('操作失败，请稍后再试！');
            }
        }
    }

    /**
     * 产品管理-产品信息
     */
    public function product(){
        $db_wash_consumer_product = M('wash_consumer_product');//实例化产品信息表

        $class_list = $db_wash_consumer_product->where("status=1")->order('id DESC')->field('id,p_id,p_name,status,name,img_url')->select();
        if($class_list){
            $class_list1 = $class_list;
        }else{
            $class_list1 = array();
        }
        $class_list = $db_wash_consumer_product->where("status=2")->order('id DESC')->field('id,p_id,p_name,status,name,img_url')->select();
        if($class_list){
            $class_list2 = $class_list;
        }else{
            $class_list2 = array();
        }
        $class_list = $db_wash_consumer_product->where("status=0")->order('id DESC')->field('id,p_id,p_name,status,name,img_url')->select();
        if($class_list){
            $class_list0 = $class_list;
        }else{
            $class_list0 = array();
        }

        $arr = array_merge($class_list1,$class_list2,$class_list0);

        /* 数组分页 */
        import("ORG.Util.Page"); //导入分页类
        $count = count($arr);//获取数组长度
        $Page = new \Think\Page($count, 30);//设置每页显示条数
        $Page->rollPage = 5;//中间显示页数(如果不配置将隐藏首尾页)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
        $list = array_slice($arr, $Page->firstRow, $Page->listRows);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->meta_title = '产品管理-产品信息';//页面title信息
        $this->assign('title', '产品管理-产品信息');//页面标题
        $this->display();
    }

    /**
     * 产品管理-产品信息-编辑
     */
    public function product_edit(){
        $db_wash_consumer_product = M('wash_consumer_product');//实例化产品信息表

        $id = I('get.id');//产品ID
        $product = $db_wash_consumer_product->where("id='$id'")->find();
        $this->assign('product', $product);

        if(IS_POST){
            $name = I('post.name');

            //判断是否有文件上传
            if($_FILES["file"]["tmp_name"]){
                /*文件上传*/
                $savename = time().'_'.mt_rand(1000000,9999999);
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 20480000;// 设置附件上传大小 2M=2048000
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = '../../Uploads/'; // 设置附件上传根目录
                $upload->savePath = './Consumer_sfa/product/picture/'; // 设置附件上传（子）目录
                $upload->saveName = $savename; // 上传文件的文件名
                //$upload->saveExt = 'jpg'; // 上传文件的保存后缀，不设置的话使用原文件后缀
                $upload->autoSub = false; // true会自动在目录文件夹下生成以日期命名的子文件夹
                $upload->replace = true; // true会存在同名文件将覆盖
                // 上传文件
                $upload_info = $upload->upload();
                if (!$upload_info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{
                    $img_url = "/wash_helper/Uploads/Consumer_sfa/product/picture/".$upload_info['file']['savename'];
                    $info['img_url'] = $img_url;

                    /*删除文件*/
                    $product_url_path = '..'.$product['img_url'];
                    if(file_exists($product_url_path)){
                        unlink($product_url_path);
                    }
                }
            }

            $info['name'] = $name;
            $info['time'] = time();
            $info['datetime'] = date('Y-m-d H:i:s');
            $edit_wash_consumer_product = $db_wash_consumer_product->where("id='$id'")->setField($info);
            if($edit_wash_consumer_product){
                $this->redirect('QRcode/product');
            }else{
                $this->error('保存失败!');
            }
        }

        $this->meta_title = '产品管理-产品信息-编辑';//页面title信息
        $this->assign('title', '产品管理-产品信息-编辑');//页面标题
        $this->display();
    }

    /**
     * 二维码-二维码生成
     */
    public function qrcode_product(){
        $db_wash_consumer_product = M('wash_consumer_product');//实例化产品信息表

        $p_id = I('get.p_id');
        if($p_id){
            $where['p_id'] = $p_id;
            $search_info['p_id'] = $p_id;
        }
        $keyword = I('get.keyword');
        if($keyword){
            $con['p_name']  = array('like', "%$keyword%");
            $con['name']  = array('like', "%$keyword%");
            $con['_logic'] = 'or';
            $where['_complex'] = $con;

            $search_info['keyword'] = $keyword;
        }
        $this->assign('search_info', $search_info);

        $where['status'] = array('eq', 1);
        $where['name'] = array('neq', '');
        $where['img_url'] = array('neq', '');
        $product = $db_wash_consumer_product->where($where)->order('datetime DESC')->field('id,p_id,p_name,name,img_url')->select();

        $arr = $product;
        $this->assign('list', $arr);

        $this->meta_title = '二维码-二维码生成';//页面title信息
        $this->assign('title', '二维码-二维码生成');//页面标题
        $this->display();
    }

    /**
     * 二维码-二维码创建
     */
    public function qrcode_create(){
        $db_wash_consumer_product = M('wash_consumer_product');//实例化产品信息表
        $db_wash_qrcode = M('wash_qrcode');//实例化二维码信息表
        $db_wash_pici = M('wash_pici');//实例化二维码批次表
        $image = new \Think\Image();

        $id = I('get.id');//产品ID
        $product = $db_wash_consumer_product->where("id='$id'")->find();
        $this->assign('product', $product);

        $p_id = $product['p_id'];//产品编号
        //查询该产品是否存在批次
        $pici = $db_wash_pici->where("p_id='$p_id'")->order('pici_id DESC')->find();
        if($pici){
            $new_pici = $pici['pici_id'] + 1;
        }else{
            $new_pici = 1;
        }
        $this->assign('new_pici', $new_pici);
        $pici_show = $product['name'].'-第'.$new_pici.'批';
        $this->assign('pici_show', $pici_show);

        if(IS_POST){
            $num = I('post.num');
            if(!$num){
                $this->error('生成数量不能为空');
            }
            if(!(is_numeric($num) && !strpos($num, '.'))){
                $this->error('生成数量必须为整数');
            }
            if($num > 300){
                $this->error('生成数量不能大于300');
            }
            $pici_name = I('post.pici_name');
            if(!$pici_name){
                $this->error('批次名称不能为空');
            }
            if(mb_strlen($pici_name,'utf-8') > 26){
                $this->error('批次名称最大不能超过26个字符，当前字符数为'.mb_strlen($pici_name,'utf-8'));
            }
            $qrcode_type = I('post.qrcode_type');
            if(!$qrcode_type){
                $this->error('请选择二维码样式');
            }

            //二维码文件批次名
            $pici_file_name = $p_id.'P'.$new_pici;
            //检测二维码图片路径
            $QR_path = '../wash_helper/Uploads/QRcode/image/'.$pici_file_name.'/';
            if(!file_exists($QR_path)){
                mkdir($QR_path, 0777);
                chmod($QR_path, 0777);
            }

            if($qrcode_type == 1){
                $LOGO = '';//二维码中的logo图片路径

                for($i=1; $i<=$num; $i++){
                    /*生成二维码*/
                    $qrcode_id = $p_id.'P'.$new_pici.'N'.$i;//二维码编号
                    $QRcode_url = 'https://app1.bluemoon.com.cn/sfaCustomer_web/index.html?id='.$qrcode_id; //二维码链接
                    $QR = $QR_path.$qrcode_id.'.png';//二维码文件路径
                    $qrcode_src = substr($QR, 2);
                    $QRCODE = $this->QRCODE($QRcode_url,$QR,$LOGO);

                    if($QRCODE == true){
                        //写入信息表
                        $info['p_id'] = $p_id;
                        $info['pici_id'] = $new_pici;
                        $info['qrcode_id'] = $qrcode_id;
                        $info['qrcode_url'] = $QRcode_url;
                        $info['qrcode_src'] = $qrcode_src;
                        $info['status'] = 0;
                        $info['qrcode_type'] = $qrcode_type;
                        $info['create_time'] = date('Y-m-d H:i:s');
                        $info['update_time'] = date('Y-m-d H:i:s');
                        $add_wash_qrcode = $db_wash_qrcode->data($info)->add();
                        if(!$add_wash_qrcode){
                            $this->error('二维码信息表写入失败!');
                        }
                    }else{
                        $this->error('生成二维码图片失败!');
                    }
                }
            }elseif($qrcode_type == 2){
                $LOGO = '..'.$product['img_url'];//二维码中的logo图片路径

                for($i=1; $i<=$num; $i++){
                    /*生成二维码*/
                    $qrcode_id = $p_id.'P'.$new_pici.'N'.$i;//二维码编号
                    $QRcode_url = 'https://app1.bluemoon.com.cn/sfaCustomer_web/index.html?id='.$qrcode_id; //二维码链接
                    $QR = $QR_path.$qrcode_id.'.png';//二维码文件路径
                    $qrcode_src = substr($QR, 2);
                    $QRCODE = $this->QRCODE($QRcode_url,$QR,$LOGO);

                    if($QRCODE == true){
                        //写入信息表
                        $info['p_id'] = $p_id;
                        $info['pici_id'] = $new_pici;
                        $info['qrcode_id'] = $qrcode_id;
                        $info['qrcode_url'] = $QRcode_url;
                        $info['qrcode_src'] = $qrcode_src;
                        $info['status'] = 0;
                        $info['qrcode_type'] = $qrcode_type;
                        $info['create_time'] = date('Y-m-d H:i:s');
                        $info['update_time'] = date('Y-m-d H:i:s');
                        $add_wash_qrcode = $db_wash_qrcode->data($info)->add();
                        if(!$add_wash_qrcode){
                            $this->error('二维码信息表写入失败!');
                        }
                    }else{
                        $this->error('生成二维码图片失败!');
                    }
                }
            }elseif($qrcode_type == 3){
                $LOGO = '..'.$product['img_url'];//二维码中的logo图片路径

                for($i=1; $i<=$num; $i++){
                    /*生成二维码*/
                    $qrcode_id = $p_id.'P'.$new_pici.'N'.$i;//二维码编号
                    $QRcode_url = 'https://app1.bluemoon.com.cn/sfaCustomer_web/index.html?id='.$qrcode_id; //二维码链接
                    $QR = $QR_path.$qrcode_id.'-base.png';//二维码文件路径
                    $QRCODE = $this->QRCODE($QRcode_url,$QR,$LOGO);

                    if($QRCODE == true) {
                        /*合成图片*/
                        $t_height = 143;//设置总高度
                        $t_width = 123;//设置总宽度
                        //创建图片
                        $save_name = $QR_path . $qrcode_id . '.png';//二维码文件路径
                        $qrcode_src = substr($save_name, 2);
                        $this->create_img($t_width, $t_height, $save_name);
                        //水印图片
                        $image->open($save_name)->water($QR, array(0, 0), 100)->save($save_name);
                        //水印文字
                        $text1 = mb_substr($product['name'],0,14,'utf-8');
                        $text2 = mb_substr($product['name'],14,null,'utf-8');
                        $image->open($save_name)->text($text1, './ThinkPHP/Library/Think/QiYe/ttfs/pfht-zhun-jian.ttf', 8, '#000000', array(6, 118))->save($save_name);
                        $image->open($save_name)->text($text2, './ThinkPHP/Library/Think/QiYe/ttfs/pfht-zhun-jian.ttf', 8, '#000000', array(6, 131))->save($save_name);
                        //删除原始二维码图
                        unlink($QR);
                    }else{
                        $this->error('生成二维码图片失败!');
                    }

                    if(!file_exists($QR)){
                        //写入信息表
                        $info['p_id'] = $p_id;
                        $info['pici_id'] = $new_pici;
                        $info['qrcode_id'] = $qrcode_id;
                        $info['qrcode_url'] = $QRcode_url;
                        $info['qrcode_src'] = $qrcode_src;
                        $info['status'] = 0;
                        $info['qrcode_type'] = $qrcode_type;
                        $info['create_time'] = date('Y-m-d H:i:s');
                        $info['update_time'] = date('Y-m-d H:i:s');
                        $add_wash_qrcode = $db_wash_qrcode->data($info)->add();
                        if(!$add_wash_qrcode){
                            $this->error('二维码信息表写入失败!');
                        }
                    }else{
                        $this->error('合成二维码图片异常!');
                    }
                }
            }elseif($qrcode_type == 4){
                $LOGO = '..'.$product['img_url'];//二维码中的logo图片路径

                for($i=1; $i<=$num; $i++){
                    /*生成二维码*/
                    $qrcode_id = $p_id.'P'.$new_pici.'N'.$i;//二维码编号
                    $QRcode_url = 'https://app1.bluemoon.com.cn/sfaCustomer_web/index.html?id='.$qrcode_id; //二维码链接
                    $QR = $QR_path.$qrcode_id.'-base.png';//二维码文件路径
                    $QRCODE = $this->QRCODE($QRcode_url,$QR,$LOGO);

                    if($QRCODE == true) {
                        /*合成图片*/
                        $t_height = 143;//设置总高度
                        $t_width = 123;//设置总宽度
                        //创建图片
                        $save_name = $QR_path . $qrcode_id . '.png';//二维码文件路径
                        $qrcode_src = substr($save_name, 2);
                        $this->create_img($t_width, $t_height, $save_name);
                        //水印图片
                        $image->open($save_name)->water($QR, array(0, 0), 100)->save($save_name);
                        //水印文字
                        $text1 = mb_substr($pici_name,0,14,'utf-8');
                        $text2 = mb_substr($pici_name,14,null,'utf-8');
                        $image->open($save_name)->text($text1, './ThinkPHP/Library/Think/QiYe/ttfs/pfht-zhun-jian.ttf', 8, '#000000', array(6, 118))->save($save_name);
                        $image->open($save_name)->text($text2, './ThinkPHP/Library/Think/QiYe/ttfs/pfht-zhun-jian.ttf', 8, '#000000', array(6, 131))->save($save_name);
                        //删除原始二维码图
                        unlink($QR);
                    }else{
                        $this->error('生成二维码图片失败!');
                    }

                    if(!file_exists($QR)){
                        //写入信息表
                        $info['p_id'] = $p_id;
                        $info['pici_id'] = $new_pici;
                        $info['qrcode_id'] = $qrcode_id;
                        $info['qrcode_url'] = $QRcode_url;
                        $info['qrcode_src'] = $qrcode_src;
                        $info['status'] = 0;
                        $info['qrcode_type'] = $qrcode_type;
                        $info['create_time'] = date('Y-m-d H:i:s');
                        $info['update_time'] = date('Y-m-d H:i:s');
                        $add_wash_qrcode = $db_wash_qrcode->data($info)->add();
                        if(!$add_wash_qrcode){
                            $this->error('二维码信息表写入失败!');
                        }
                    }else{
                        $this->error('合成二维码图片异常!');
                    }
                }
            }else{
                $this->error('二维码样式参数格式错误');
            }

            //写入批次表
            $info['p_id'] = $p_id;
            $info['pici_id'] = $new_pici;
            $info['pici_name'] = $pici_name;
            $info['create_num'] = $num;
            $info['cuxiao_num'] = 0;
            $info['xiaofei_num'] = 0;
            $info['qrcode_type'] = $qrcode_type;
            $info['create_time'] = date('Y-m-d H:i:s');
            $info['update_time'] = date('Y-m-d H:i:s');
            $add_wash_pici = $db_wash_pici->data($info)->add();
            if($add_wash_pici){
                $this->success('二维码批量生成成功~',U('QRcode/qrcode_admin'));
            }else{
                $this->error('二维码批次表写入失败!');
            }
        }

        $this->meta_title = '二维码-二维码创建';//页面title信息
        $this->assign('title', '二维码-二维码创建');//页面标题
        $this->display();
    }

    /**
     * 二维码-二维码管理
     */
    public function qrcode_admin(){
        $db_wash_consumer_product = M('wash_consumer_product');//实例化产品信息表
        $db_wash_pici = M('wash_pici');//实例化二维码批次表

        $pici_id = I('get.pici_id');
        if($pici_id){
            $where_info['pici_id'] = $pici_id;
            $search_info['pici_id'] = $pici_id;
        }
        $pici_name = I('get.pici_name');
        if($pici_name){
            $where_info['pici_name'] = $pici_name;
            $search_info['pici_name'] = $pici_name;
        }
        $p_id = I('get.p_id');
        if($p_id){
            $where_info['p_id'] = $p_id;
            $search_info['p_id'] = $p_id;
        }
        $name = I('get.name');
        if($name){
            $get_product = $db_wash_consumer_product->where("name='$name'")->field('p_id')->find();
            if($get_product){
                if($p_id){
                    if($get_product['p_id'] == $p_id){
                        $where_info['p_id'] = $get_product['p_id'];
                    }else{
                        $where_info['p_id'] = '';
                    }
                }else{
                    $where_info['p_id'] = $get_product['p_id'];
                }
            }else{
                $where_info['p_id'] = '';
            }
            $search_info['name'] = $name;
        }
        $this->assign('search_info', $search_info);

        $pici_info = $db_wash_pici->where($where_info)->order('pici_id DESC,create_time DESC')->field('id,p_id,pici_id,pici_name,create_num,cuxiao_num,xiaofei_num,create_time')->select();
        $number = 1;//序号初始化
        foreach($pici_info as $k=>$v){
            $p_id = $v['p_id'];
            $product = $db_wash_consumer_product->where("p_id='$p_id'")->field('name')->find();
            if($product){
                $name = $product['name'];
                $pici_info[$k]['name'] = $name;
                $pici_info[$k]['number'] = $number;
                $number += 1;
            }else{
                $this->error('产品编码不存在!');
            }
        }
        $arr = $pici_info;

        /* 数组分页 */
        import("ORG.Util.Page"); //导入分页类
        $count = count($arr);//获取数组长度
        $Page = new \Think\Page($count, 20);//设置每页显示条数
        $Page->rollPage = 5;//中间显示页数(如果不配置将隐藏首尾页)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
        $list = array_slice($arr, $Page->firstRow, $Page->listRows);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->meta_title = '二维码-二维码管理';//页面title信息
        $this->assign('title', '二维码-二维码管理');//页面标题
        $this->display();
    }

    /**
     * 二维码-批次详情
     */
    public function pici_detail(){
        $db_wash_qrcode = M('wash_qrcode');//实例化二维码信息表
        $db_wash_pici = M('wash_pici');//实例化二维码批次表
        $db_wash_qy_user = M('wash_qy_user');//实例化企业用户信息表
        $db_wash_user = M('wash_user');//实例化用户信息表

        $id = I('get.id');
        $qrcode_id = I('get.qrcode_id');
        if($qrcode_id){
            $where_info['qrcode_id'] = $qrcode_id;
            $search_info['qrcode_id'] = $qrcode_id;
        }
        $qy_uid = I('get.qy_uid');
        if($qy_uid){
            $where_info['qy_uid'] = $qy_uid;
            $search_info['qy_uid'] = $qy_uid;
        }
        $qy_name = I('get.qy_name');
        if($qy_name){
            $qy_info = $db_wash_qy_user->where("name='$qy_name'")->field('userid')->find();
            if($qy_info){
                if($qy_uid){
                    if($qy_info['userid'] == $qy_uid){
                        $where_info['qy_uid'] = $qy_info['userid'];
                    }else{
                        $where_info['qy_uid'] = 'false';
                    }
                }else{
                    $where_info['qy_uid'] = $qy_info['userid'];
                }
            }else{
                $where_info['qy_uid'] = 'false';
            }
            $search_info['qy_name'] = $qy_name;
        }
        $type = I('get.type');
        if($type){
            if($type == 1){
                $where_info['status'] = 0;
            }elseif($type == 2){
                $where_info['status'] = 1;
            }elseif($type == 3){
                $where_info['status'] = 2;
            }elseif($type == 4){
                $where_info['status'] = 3;
            }

            $search_info['type'] = $type;
        }
        $this->assign('search_info', $search_info);

        $pici_info = $db_wash_pici->where("id='$id'")->field('id,p_id,pici_id,pici_name')->find();
        if($pici_info){
            $where_info['p_id'] = $pici_info['p_id'];
            $where_info['pici_id'] = $pici_info['pici_id'];

            $this->assign('pici_info', $pici_info);
        }else{
            $this->error('批次ID信息不存在!');
        }

        $qrcode_list = $db_wash_qrcode->where($where_info)->field('qrcode_id,status,qy_uid,bangding_time,uid,tijiao_time,tuihuo_reason,tuihuo_time')->select();
        $number = 1;//序号初始化
        foreach($qrcode_list as $k=>$v){
            $cuxiao_uid = $v['qy_uid'];
            $cuxiao_info = $db_wash_qy_user->where("userid='$cuxiao_uid'")->field('name')->find();
            if($cuxiao_info){
                $qrcode_list[$k]['qy_name'] = $cuxiao_info['name'];
            }
            $xiaofei_uid = $v['uid'];
            $xiaofei_info = $db_wash_user->where("uid='$xiaofei_uid'")->field('nickName')->find();
            if($xiaofei_info){
                $qrcode_list[$k]['nickName'] = $xiaofei_info['nickName'];
            }
            $qrcode_list[$k]['number'] = $number;
            $number += 1;
        }
        $arr = $qrcode_list;

        /* 数组分页 */
        import("ORG.Util.Page"); //导入分页类
        $count = count($arr);//获取数组长度
        $Page = new \Think\Page($count, 20);//设置每页显示条数
        $Page->rollPage = 5;//中间显示页数(如果不配置将隐藏首尾页)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $show = $Page->show();// 分页显示输出
        $list = array_slice($arr, $Page->firstRow, $Page->listRows);
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->meta_title = '二维码-批次详情';//页面title信息
        $this->assign('title', '二维码-批次详情');//页面标题
        $this->display();
    }

    /**
     * 二维码-二维码详情
     */
    public function qrcode_detail(){
        $db_wash_pici = M('wash_pici');//实例化二维码批次表

        $id = I('post.id');
        $pici = $db_wash_pici->where("id='$id'")->field('p_id,pici_id,create_num')->find();
        if($pici){
            $p_id = $pici['p_id'];
            $pici_id = $pici['pici_id'];
            $create_num = $pici['create_num'];
            //二维码文件批次名
            $pici_file_name = $p_id.'P'.$pici_id;

            if(!file_exists('./Uploads/QRcode/pdf/'.$pici_file_name.'.pdf')){
                //编辑PDF内容
                $head = '<table cellpadding="0" cellspacing="0" border="0" style="align:center;">';
                $mid = '';
                for($i=1; $i<=$create_num; $i++){
                    //二维码编号
                    $qrcode_id = $p_id.'P'.$pici_id.'N'.$i;

                    if($i % 6 == 1){
                        $mid .= '<tr>';
                        $mid .= '<td><img src="./Uploads/QRcode/image/'.$pici_file_name.'/'.$qrcode_id.'.png"></td>';
                    }elseif($i % 6 == 0){
                        $mid .= '<td><img src="./Uploads/QRcode/image/'.$pici_file_name.'/'.$qrcode_id.'.png"></td>';
                        $mid .= '</tr>';
                    }else{
                        $mid .= '<td><img src="./Uploads/QRcode/image/'.$pici_file_name.'/'.$qrcode_id.'.png"></td>';
                    }
                }
                $tail = '</tr></table>';
                $content = $head.$mid.$tail;

                $this->QRCODE_PDF($p_id,$pici_id,$content);
            }

            echo $pici_file_name.'.pdf';
        }
    }

    /**
     * 二维码-二维码压缩包下载
     */
    public function qrcode_download(){
        $db_wash_pici = M('wash_pici');//实例化二维码批次表

        $id = I('post.id');
        $pici = $db_wash_pici->where("id='$id'")->field('p_id,pici_id')->find();
        if($pici){
            $p_id = $pici['p_id'];
            $pici_id = $pici['pici_id'];
            //二维码文件批次名
            $pici_file_name = $p_id.'P'.$pici_id;
            //压缩文件路径
            $zip_url_path = './Uploads/QRcode/zip/'.$pici_file_name.'.zip';

            if(!file_exists($zip_url_path)){
                fopen($zip_url_path, "w");

                //实例化
                $zip = new \ZipArchive();
                $ZipFile = new \Vendor\zip\Zip();

                if($zip->open($zip_url_path, \ZipArchive::OVERWRITE)=== TRUE){
                    //调用方法，对要打包的根目录进行操作，并将ZipArchive的对象传递给方法
                    $ZipFile->addFileToZip('./Uploads/QRcode/image/'.$pici_file_name, $zip);
                    $zip->close(); //关闭处理的zip文件
                }
            }

            echo $pici_file_name.'.zip';
        }
    }

    /**
     * 二维码-二维码批次删除
     */
    public function qrcode_del(){
        $db_wash_pici = M('wash_pici');//实例化二维码批次表
        $db_wash_qrcode = M('wash_qrcode');//实例化二维码信息表

        $id = I('post.id');
        $pici = $db_wash_pici->where("id='$id'")->field('p_id,pici_id,create_num,cuxiao_num,xiaofei_num')->find();
        if($pici){
            $p_id = $pici['p_id'];
            $pici_id = $pici['pici_id'];
            $create_num = $pici['create_num'];
            $cuxiao_num = $pici['cuxiao_num'];
            $xiaofei_num = $pici['xiaofei_num'];

            if($cuxiao_num != 0){
                exit('405');
            }
            if($xiaofei_num != 0){
                exit('406');
            }

            //二维码文件批次名
            $pici_file_name = $p_id.'P'.$pici_id;

            for($i=1; $i<=$create_num; $i++){
                $qrcode_id = $pici_file_name.'N'.$i;

                //删除二维码信息
                $del_wash_qrcode = $db_wash_qrcode->where("qrcode_id='$qrcode_id'")->delete();
                if(!$del_wash_qrcode){
                    exit('400');
                }
            }

            //删除二维码批次
            $del_wash_pici = $db_wash_pici->where("id='$id'")->delete();
            if(!$del_wash_pici){
                exit('401');
            }

            //删除二维码文件夹
            $path = './Uploads/QRcode/image/'.$pici_file_name;
            if(file_exists($path)){
                $del = $this->delDirAndFile($path);
                if($del != true){
                    exit('402');
                }
            }else{
                exit('403');
            }

            //删除pdf文件
            $pdf_pash = './Uploads/QRcode/pdf/'.$pici_file_name.'.pdf';
            if(file_exists($pdf_pash)){
                unlink($pdf_pash);
            }

            //删除zip文件
            $zip_pash = './Uploads/QRcode/zip/'.$pici_file_name.'.zip';
            if(file_exists($zip_pash)){
                unlink($zip_pash);
            }

            echo '200';
        }else{
            exit('404');
        }
    }

    /**
     * 生成二维码
     * string  $QRcode_url  二维码链接
     * string  $QR          二维码文件地址
     * string  $LOGO        二维码logo地址
     */
    public function QRCODE($QRcode_url,$QR,$LOGO=''){
        //配置
        $QRcode = new \Vendor\qrcode\QRcode();
        $errorCorrectionLevel = 'M';//容错级别L、M、Q、H有低到高
        $matrixPointSize = 3;//生成图片大小

        $QRcode->png($QRcode_url, $QR, $errorCorrectionLevel, $matrixPointSize, 2);

        if (file_exists($LOGO)) {
            $QR_logo = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($LOGO));
            $QR_width = imagesx($QR_logo);//二维码图片宽度
            $QR_height = imagesy($QR_logo);//二维码图片高度
            $logo_width = imagesx($logo);//logo图片宽度
            $logo_height = imagesy($logo);//logo图片高度
            $logo_qr_width = $QR_width / 4;
            $scale = $logo_width/$logo_qr_width;
            $logo_qr_height = $logo_height/$scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($QR_logo, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
                $logo_qr_width, $logo_width, $logo_height);
            //输出二维码水印图
            imagepng($QR_logo, $QR);
        }

        return true;
    }

    /**
     * 生成二维码pdf文件
     * string  $p_id        产品编号
     * int     $pici_id     批次号
     * string  $content     内容
     */
    public function QRCODE_PDF($p_id,$pici_id,$content=''){
        //二维码文件批次名
        $pici_file_name = $p_id.'P'.$pici_id;

        //引入文件
        require './ThinkPHP/Library/Vendor/TCPDF/tcpdf.php';
        //实例化
        $pdf = new \TCPDF();
        // 设置文档信息
        $pdf->SetCreator('bluemoon');
        $pdf->SetAuthor('liuyang');
        $pdf->SetTitle('消费者二维码 - '.$pici_file_name);
        $pdf->SetSubject('TCPDF Tutorial');
        $pdf->SetKeywords('TCPDF, PDF, PHP');
        // 设置页眉和页脚信息
        $pdf->SetHeaderData('', '', '蓝月亮（中国）有限公司 - 消费者二维码                                                                                                    产品编号：'.$p_id.'      批次号：'.$pici_id, '', array(0,64,255), array(0,64,128));
        $pdf->setFooterData(array(0,64,0), array(0,64,128));
        // 设置页眉和页脚字体
        $pdf->setHeaderFont(Array('stsongstdlight', '', '9'));
        $pdf->setFooterFont(Array('helvetica', '', '9'));
        // 设置默认等宽字体
        $pdf->SetDefaultMonospacedFont('courier');
        // 设置间距
        $pdf->SetMargins(5, 8, 5);
        $pdf->SetHeaderMargin(2);
        $pdf->SetFooterMargin(6);
        // 设置分页
        $pdf->SetAutoPageBreak(TRUE);
        //设置自动换页
//        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        // 设置图片比例
        $pdf->setImageScale(1.25);
        // 设置默认字体子集设置模式
        $pdf->setFontSubsetting(true);
        //设置字体
        $pdf->SetFont('stsongstdlight', '', 14);
        //添加一个页面
        $pdf->AddPage();
        //写入内容
        $pdf->writeHTML($content);
        //输出PDF 默认是I：在浏览器中打开，D：下载，F：在服务器生成pdf ，S：只返回pdf的字符串
        $pdf->Output('./Uploads/QRcode/pdf/'.$pici_file_name.'.pdf', 'F');
    }

    /**
     * 删除图文混排图片
     * string  $str    要提去的字符串
     * int     $type   1提取图片 2提取src和href
     */
    public function del_images_path($str,$type=1){
        if($type == 1){
            preg_match('/<img.+src=\"?(.+\.(jpg|gif|jpeg|png))\"?.+>/i',$str,$match);
            if($match[1]){
                foreach($match[1] as $v){
                    $images_path = '..'.$v;
                    if(file_exists($images_path)){
                        unlink($images_path);
                    }
                }
            }
        }
        if($type == 2){
            preg_match_all('/(src|href)=\"?(.+\.(jpg|gif|jpeg|png))\"/i',$str,$match);
            if($match[2]){
                foreach($match[2] as $v){
                    $images_path = '..'.$v;
                    if(file_exists($images_path)){
                        unlink($images_path);
                    }
                }
            }
        }
    }

    /**
     * 创建图片
     * int     width    宽
     * int     height   高
     * string  saveImg  存储地址
     */
    public function create_img($width,$height,$saveImg){
        //创建画布
        $img = imagecreatetruecolor($width,$height);
        //背景色
        $bg_color = imagecolorallocate($img,255,255,255);
        //在画布上染色
        imagefill($img,0,0,$bg_color);
        //输出图像
        imagepng($img,$saveImg);
        //释放画布资源
        imagedestroy($img);
    }

    /**
     * 网络图片保存到本地
     * string  url  网络图片地址
     * string  filename  存储地址
     */
    public function img_exists($url,$filename){
        $state = @file_get_contents($url,0,null,0,1);//获取网络资源的字符内容
        if($state){
            ob_start();//打开输出
            readfile($url);//输出图片文件
            $img = ob_get_contents();//得到浏览器输出
            ob_end_clean();//清除输出并关闭
            $fp = @fopen($filename, "a");
            fwrite($fp, $img);//向当前目录写入图片文件，并重新命名
            fclose($fp);
            return true;
        }else{
            return false;
        }
    }

    /**
     * 循环删除目录和子文件
     * string  $dirName  文件路径
     */
    public function delDirAndFile($dirName){
        if($handle = opendir($dirName)){
            while(false !== ($item = readdir($handle))){
                if($item != "." && $item != ".."){
                    if (is_dir( "$dirName/$item")) {
                        delDirAndFile("$dirName/$item");
                    }else{
                        //删除文件
                        unlink("$dirName/$item");
                    }
                }
            }
            closedir($handle);

            //删除空文件夹
            rmdir($dirName);

            return true;
        }
    }
}
